import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import '../../utils/http.dart';
import '../../utils/toast.dart';

class MinePage extends StatefulWidget {
  MinePage({super.key, required this.currentIndex});

  int currentIndex;

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  @override
  void didUpdateWidget(covariant MinePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.currentIndex == 2) {
      // TODO: 调用接口获取数据
      getTaskData();
      getUserInfo();
    }
  }

  Map taskData = {
    'completedAmounts': 0,
    'taskAmounts': 0,
    'transportMileage': 0,
    'dailyMileage': []
  };

  Map userInfo = {'name': '', 'avatar': '', 'number': '', 'phone': ''};

  getTaskData() async {
    DateTime now = DateTime.now();
    String currentYear = now.year.toString();
    String currentMonth = now.month.toString();
    var res = await http.get('/driver/users/taskReport', params: {
      'month': currentMonth,
      'year': currentYear,
    });
    if (res['code'] != 200) return ToastUtil.showError('获取数据失败');
    setState(() {
      taskData = res['data'];
    });
  }

  getUserInfo() async {
    var res = await http.get('/driver/users');
    if (res['code'] != 200) return ToastUtil.showError('获取数据失败');
    setState(() {
      userInfo = res['data'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: const Color.fromARGB(200, 239, 237, 237),
        body: Column(children: [
          Stack(children: [
            Container(
              width: double.infinity,
              height: MediaQuery.of(context).size.height * 0.45,
              decoration: BoxDecoration(
                color: Color.fromARGB(200, 246, 245, 245),
                image: DecorationImage(
                    image: AssetImage('assets/mine/bg_page_my.jpg'),
                    fit: BoxFit.cover),
              ),
            ),
            Column(
              children: [
                const SizedBox(height: 150),
                ClipRRect(
                  borderRadius: BorderRadius.circular(50.0),
                  child: userInfo['avatar'] != ''
                      ? Image.network(userInfo['avatar'], width: 80, height: 80)
                      : SvgPicture.asset(
                          'assets/mine/ic_avatar_driver.svg',
                          width: 80,
                          height: 80,
                        ),
                ),
                const SizedBox(height: 10),
                Text(
                  userInfo['name'] != '' ? userInfo['name'] : '未登录',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '司机编号：',
                      style: TextStyle(color: Colors.white, fontSize: 15),
                    ),
                    Text(
                      userInfo['number'] != '' ? userInfo['number'] : '未登录',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '手机号：',
                      style: TextStyle(color: Colors.white, fontSize: 15),
                    ),
                    Text(
                      userInfo['phone'] != '' ? userInfo['phone'] : '未登录',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 30),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20)),
                  child: Column(
                    children: [
                      const SizedBox(height: 20),
                      Text(
                        '-本月任务-',
                        style: TextStyle(color: Colors.grey, fontSize: 15),
                        textAlign: TextAlign.center,
                      ),
                      const SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Column(children: [
                            Text(
                              taskData['taskAmounts'].toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold),
                            ),
                            const SizedBox(height: 10),
                            Text(
                              '任务总量',
                              style:
                                  TextStyle(color: Colors.black, fontSize: 12),
                            ),
                          ]),
                          Column(children: [
                            Text(
                              taskData['completedAmounts'].toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold),
                            ),
                            const SizedBox(height: 10),
                            Text(
                              '完成任务量',
                              style:
                                  TextStyle(color: Colors.black, fontSize: 12),
                            ),
                          ]),
                          Column(children: [
                            Text(
                              taskData['transportMileage'].toString(),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold),
                            ),
                            const SizedBox(height: 10),
                            Text(
                              '运输里程(km)',
                              style:
                                  TextStyle(color: Colors.black, fontSize: 12),
                            ),
                          ]),
                        ],
                      ),
                      const SizedBox(height: 30),
                    ],
                  ),
                ),
                const SizedBox(height: 20),
                Container(
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    padding: EdgeInsets.all(20),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20)),
                    child: Column(children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '车辆信息',
                            style: TextStyle(color: Colors.black, fontSize: 15),
                            textAlign: TextAlign.center,
                          ),
                          SvgPicture.asset(
                            'assets/mine/ic_btn_more.svg',
                            color: Colors.red,
                            width: 20,
                            height: 20,
                          ),
                        ],
                      ),
                      const SizedBox(height: 30),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '任务数据',
                            style: TextStyle(color: Colors.black, fontSize: 15),
                            textAlign: TextAlign.center,
                          ),
                          SvgPicture.asset(
                            'assets/mine/ic_btn_more.svg',
                            color: Colors.red,
                            width: 20,
                            height: 20,
                          ),
                        ],
                      ),
                      const SizedBox(height: 30),
                      GestureDetector(
                          onTap: () {
                            Navigator.pushNamed(context, '/setting');
                          },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '系统设置',
                                style: TextStyle(
                                    color: Colors.black, fontSize: 15),
                                textAlign: TextAlign.center,
                              ),
                              SvgPicture.asset(
                                'assets/mine/ic_btn_more.svg',
                                color: Colors.red,
                                width: 20,
                                height: 20,
                              ),
                            ],
                          ))
                    ])),
              ],
            ),
          ]),
        ]));
  }
}
